package com.example.mywiseelves.fragment;

import android.content.ContentValues;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.mywiseelves.R;
import com.example.mywiseelves.bean.UserBean;
import com.example.mywiseelves.dao.UserDao;
import com.example.mywiseelves.view.TreeView;

import java.util.Random;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link HomeFragment#newInstance} factory method to
 * create an instance of this fragment.
 * 祈愿树界面
 */
public class HomeFragment extends Fragment implements View.OnClickListener{

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    //我的祈愿树
    TreeView treeView;

    //用户dao对象
    UserDao userDao;


    View view;
    public HomeFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment HomeFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static HomeFragment newInstance(String param1, String param2) {
        HomeFragment fragment = new HomeFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
        userDao=new UserDao(getContext());

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view=inflater.inflate(R.layout.fragment_home, container, false);
        // Inflate the layout for this fragment
        findViews();
        treeView.setOnClickListener(this);
        return view;
    }


    /**
     * 实例化控件
     * */
    private void findViews() {
        treeView=view.findViewById(R.id.myTree_tv);
    }


    @Override
    public void onClick(View v) {
        if (v.getId()==R.id.myTree_tv){
            //查询用户表信息
            UserBean userBean=userDao.userQuery();
            int user_point=userBean.getUser_point();
            int user_pray = userBean.getUser_pray();
            if (userBean.getUser_pray()<1){
                //判断祈愿状态
                Random r = new Random();
                int i1 = r.nextInt(999 - 99) + 99;
                userBean.setUser_point(user_point+i1);
                userBean.setUser_pray(user_pray+1);
                userDao.userPointUpdate1(userBean);

                Toast.makeText(getContext(),userBean.getUser_name()+",祈愿成功，获得："+i1+"祈愿点"+",当前祈愿次数："+userBean.getUser_pray()+"次", Toast.LENGTH_LONG).show();
            }else{
                Toast.makeText(getContext(),"今日已到达祈愿最大次数,当前祈愿次数："+userBean.getUser_pray()+"次", Toast.LENGTH_LONG).show();
            }


        }
    }
}